package com.xhwl.logistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.pojo.cent.vehicle.VehcCarRecord;
import com.xhwl.common.pojo.cent.vehicle.VehcParkingLot;
import com.xhwl.logistics.bo.VehicleCarportBO;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.bo.input.QueryVehicleAccessRecordStatisticsBO;
import com.xhwl.logistics.bo.VehiclePayRecordStatisticsBO;
import com.xhwl.logistics.bo.VehicleAccessRecordStatisticsBO;
import com.xhwl.logistics.bo.input.QueryVehicleCarportBO;
import com.xhwl.logistics.bo.input.QueryVehiclePayRecordStatisticsBO;
import com.xhwl.logistics.component.LookupVehicleParkingLotHandler;
import com.xhwl.logistics.constant.VehicleConstant;
import com.xhwl.logistics.converter.ToVehiclePayRecordStatisticsBO;
import com.xhwl.logistics.dao.VehcCarRecordDao;
import com.xhwl.logistics.dao.VehcParkingLotDao;
import com.xhwl.logistics.dao.mapper.IVehicleAccessRecordMapper;
import com.xhwl.logistics.dao.mapper.IVehiclePayRecordMapper;
import com.xhwl.logistics.po.VehicleAccessRecordStatisticsDatePO;
import com.xhwl.logistics.po.VehiclePayRecordStatisticsDatePO;
import com.xhwl.logistics.service.IVehicleStatisticsService;
import com.xhwl.logistics.service.cent.QueryDeviceService;
import com.xhwl.logistics.service.impl.http.KeTopHttpServiceImpl;
import com.xhwl.logistics.strategy.IVehicleParkingLotHandler;
import com.xhwl.logistics.util.AboutDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Slf4j
//@Service
public class VehicleStatisticsServiceImpl implements IVehicleStatisticsService {

    @Autowired
    private VehcParkingLotDao vehcParkingLotDao;

    @Autowired
    private VehcCarRecordDao vehcCarRecordDao;

    @Autowired
    private LookupVehicleParkingLotHandler lookupVehicleParkingLotHandler;

    @Autowired
    private QueryDeviceService queryDeviceService;

    @Autowired
    private IVehicleAccessRecordMapper vehicleAccessRecordMapper;

    @Autowired
    private IVehiclePayRecordMapper vehiclePayRecordMapper;

    @Autowired
    private ToVehiclePayRecordStatisticsBO toVehiclePayRecordStatisticsBO;

    //目前没有使用动态的依赖查找来获取各种车场的HttpService的实现，而是绑定科拓的HttpService的实现，后续需要修改优化
    @Autowired
    private KeTopHttpServiceImpl keTopHttpService;

    private Wrapper<VehcParkingLot> queryParkingLotCondition(Integer projectId, String parkingLotName) {
        QueryWrapper<VehcParkingLot> queryWrapper = new QueryWrapper<>();

        if (null!=projectId) {
            queryWrapper.eq("project_id", projectId);
        }
        if (null!=parkingLotName) {
            queryWrapper.eq("name", parkingLotName);
        }
        return queryWrapper;
    }

    private Wrapper<VehcParkingLot> queryParkingLotCondition(QueryVehicleAccessRecordStatisticsBO queryVehicleAccessRecordStatisticsBO) {
        QueryWrapper<VehcParkingLot> queryWrapper = new QueryWrapper<>();

        //目前数据没有校验，所以使用判空的形式
        Integer projectId = queryVehicleAccessRecordStatisticsBO.getProjectId();
        String parkingLotName = queryVehicleAccessRecordStatisticsBO.getParkingLotName();
        if (null!= queryVehicleAccessRecordStatisticsBO.getProjectId()) {
            queryWrapper.eq("project_id", projectId);
        }
        if (null!=parkingLotName) {
            queryWrapper.eq("name", parkingLotName);
        }
        return queryWrapper;
    }

    private Wrapper<VehcCarRecord> queryTodayEnterVehicleAccessRecordCondition(String parkingId, Integer projectId) {
        QueryWrapper<VehcCarRecord> queryWrapper = new QueryWrapper<>();

        //目前数据没有校验，所以使用判空的形式
        Date now = new Date();
        Date startTime = AboutDateUtil.theSameDayOfDateAnother(now, VehicleConstant.vehicleTimeZone);
        Date endTime = AboutDateUtil.datePlusDay(startTime, 1);
        if (StringUtils.isNotEmpty(parkingId)) {
            queryWrapper.eq("third_park_id", parkingId);
        }
        queryWrapper.eq("project_id", projectId);
        queryWrapper.ge("enter_time", startTime);
        queryWrapper.le("enter_time", endTime);
        return queryWrapper;
    }

    private Wrapper<VehcCarRecord> queryTodayExistVehicleAccessRecordCondition(String parkingId, Integer projectId) {
        QueryWrapper<VehcCarRecord> queryWrapper = new QueryWrapper<>();

        //目前数据没有校验，所以使用判空的形式
        Date now = new Date();
        Date startTime = AboutDateUtil.theSameDayOfDateAnother(now, VehicleConstant.vehicleTimeZone);
        Date endTime = AboutDateUtil.datePlusDay(startTime, 1);
        if (StringUtils.isNotEmpty(parkingId)) {
            queryWrapper.eq("third_park_id", parkingId);
        }
        queryWrapper.eq("project_id", projectId);
        queryWrapper.ge("exist_time", startTime);
        queryWrapper.le("exist_time", endTime);
        return queryWrapper;
    }

    private Wrapper<VehcCarRecord> queryEnterVehicleAccessRecordCondition(String parkingId, QueryVehicleAccessRecordStatisticsBO queryVehicleAccessRecordStatisticsBO) {
        QueryWrapper<VehcCarRecord> queryWrapper = new QueryWrapper<>();

        //目前数据没有校验，所以使用判空的形式
        Date startTime = queryVehicleAccessRecordStatisticsBO.getStartTime();
        Date endTime = queryVehicleAccessRecordStatisticsBO.getEndTime();
        if (null!=parkingId) {
            queryWrapper.eq("third_park_id", parkingId);
        }
        if (null!=startTime) {
            queryWrapper.ge("enter_time", startTime);
        }
        if (null!=endTime) {
            queryWrapper.le("enter_time", endTime);
        }
        return queryWrapper;
    }

    private Wrapper<VehcCarRecord> queryExistVehicleAccessRecordCondition(String parkingId, QueryVehicleAccessRecordStatisticsBO queryVehicleAccessRecordStatisticsBO) {
        QueryWrapper<VehcCarRecord> queryWrapper = new QueryWrapper<>();

        //目前数据没有校验，所以使用判空的形式
        Date startTime = queryVehicleAccessRecordStatisticsBO.getStartTime();
        Date endTime = queryVehicleAccessRecordStatisticsBO.getEndTime();
        if (null!=parkingId) {
            queryWrapper.eq("third_park_id", parkingId);
        }
        if (null!=startTime) {
            queryWrapper.ge("exist_time", startTime);
        }
        if (null!=endTime) {
            queryWrapper.le("exist_time", endTime);
        }
        return queryWrapper;
    }

    @Override
    public VehicleAccessRecordStatisticsBO accessRecordStatistics(QueryVehicleAccessRecordStatisticsBO queryVehicleAccessRecordStatisticsBO) {
        Integer projectId = queryVehicleAccessRecordStatisticsBO.getProjectId();

        VehicleIntegratedConfigBO vehicleIntegratedConfigBO = queryDeviceService.queryIntegratedConfigFromProjectId(projectId);
        log.info("vehicleIntegratedConfigBO: " + vehicleIntegratedConfigBO);

        IVehicleParkingLotHandler vehicleParkingLotHandler = lookupVehicleParkingLotHandler.lookup(vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProductId());

        VehicleCarportBO vehicleCarportBO = vehicleParkingLotHandler.queryCarport(vehicleIntegratedConfigBO, new QueryVehicleCarportBO(projectId, queryVehicleAccessRecordStatisticsBO.getParkingLotName()));
        Integer carSpotNum = vehicleCarportBO.getTotalNumber();
        Integer freeSpaceNumber = vehicleCarportBO.getFreeNumber();

        List<VehcParkingLot> vehcParkingLotList = vehcParkingLotDao.selectList(queryParkingLotCondition(queryVehicleAccessRecordStatisticsBO));
        String parkId;
        if (!vehcParkingLotList.isEmpty()) {
            VehcParkingLot vehcParkingLot = vehcParkingLotList.get(0);
            String thirdParkId = vehcParkingLot.getThirdParkId();
            parkId = thirdParkId;
            queryVehicleAccessRecordStatisticsBO.setParkId(thirdParkId);
        }else {
            throw new BusinessException("通过项目id和车场名称，查找不出车场id");
        }

        List<VehcCarRecord> enterTodayVehicleAccessRecordList = vehcCarRecordDao.selectList(queryTodayEnterVehicleAccessRecordCondition(parkId, queryVehicleAccessRecordStatisticsBO.getProjectId()));
        List<VehcCarRecord> existTodayVehicleAccessRecordList = vehcCarRecordDao.selectList(queryTodayExistVehicleAccessRecordCondition(parkId, queryVehicleAccessRecordStatisticsBO.getProjectId()));
//        List<VehcCarRecord> enterVehicleAccessRecordList = vehcCarRecordDao.selectList(queryEnterVehicleAccessRecordCondition(thirdParkId, queryVehicleAccessRecordStatisticsBO));
//        List<VehcCarRecord> existVehicleAccessRecordList = vehcCarRecordDao.selectList(queryExistVehicleAccessRecordCondition(thirdParkId, queryVehicleAccessRecordStatisticsBO));

        List<VehicleAccessRecordStatisticsDatePO> enterVehicleAccessRecordStatisticsDatePOList = vehicleAccessRecordMapper.enterAccessRecordStatistics(queryVehicleAccessRecordStatisticsBO);
        List<VehicleAccessRecordStatisticsDatePO> existVehicleAccessRecordStatisticsDatePOList = vehicleAccessRecordMapper.existAccessRecordStatistics(queryVehicleAccessRecordStatisticsBO);

        String parkingLotName = queryVehicleAccessRecordStatisticsBO.getParkingLotName();
        String homeAddress = queryVehicleAccessRecordStatisticsBO.getHomeAddress();
        return VehicleAccessRecordStatisticsBO.toVehicleStatisticsBO(parkingLotName, carSpotNum, freeSpaceNumber, homeAddress, queryVehicleAccessRecordStatisticsBO, enterTodayVehicleAccessRecordList, existTodayVehicleAccessRecordList, enterVehicleAccessRecordStatisticsDatePOList, existVehicleAccessRecordStatisticsDatePOList);
    }

    @Override
    public VehiclePayRecordStatisticsBO payRecordStatistics(QueryVehiclePayRecordStatisticsBO queryVehiclePayRecordStatisticsBO) {
        Integer projectId = queryVehiclePayRecordStatisticsBO.getProjectId();
        String parkingLotName = queryVehiclePayRecordStatisticsBO.getParkingLotName();

        List<VehcParkingLot> vehcParkingLotList = vehcParkingLotDao.selectList(queryParkingLotCondition(projectId, parkingLotName));
        if (!vehcParkingLotList.isEmpty()) {
            VehcParkingLot vehcParkingLot = vehcParkingLotList.get(0);
            queryVehiclePayRecordStatisticsBO.setParkId(vehcParkingLot.getThirdParkId());
        }else {
            throw new BusinessException("通过项目id和车场名称，查找不出车场id");
        }

        List<VehiclePayRecordStatisticsDatePO> vehiclePayRecordStatisticsDatePOList = vehiclePayRecordMapper.payRecordStatistics(queryVehiclePayRecordStatisticsBO);

        Integer type = queryVehiclePayRecordStatisticsBO.getType();
        Date startTime = queryVehiclePayRecordStatisticsBO.getStartTime();
        Date endTime = queryVehiclePayRecordStatisticsBO.getEndTime();
        return toVehiclePayRecordStatisticsBO.toVehiclePayRecordStatisticsBO(vehiclePayRecordStatisticsDatePOList, parkingLotName, type, startTime, endTime);
    }

    @Override
    public VehicleCarportBO freeCarSpotNum(QueryVehiclePayRecordStatisticsBO queryVehiclePayRecordStatisticsBO) {
        return null;
    }

}
